
cc.Class({
    extends: cc.Component,

    properties: {
        hamburgerChild:[cc.Prefab],
        labelNum:cc.Label,
    },

    // LIFE-CYCLE CALLBACKS:
    // onLoad () {},
    start () {
     this.game = cc.find("Canvas").getComponent('Home');
     this.place = false;
     this.node.children[1].zIndex=100;
     this.drag_dc();
     this.placeNode = null;//放置的汉堡节点
     this.MakeContent ='';    // meat  tomato  onion  lettuce  cheese;
    },
    drag_dc(){
        var  clicked = 1;
        var  clickedTime={
            'timeA':" ",
            'timeB':" "
        };
        this.parentInit = null;
        this.node.on('touchstart',function(event){
            if(clicked==1){
                clickedTime.timeA=new Date().getTime();
                clicked++;
            }
            else if(clicked==2){
                clickedTime.timeB=new Date().getTime();
                if (Math.abs(clickedTime.timeA-clickedTime.timeB)<400 && this.node.childrenCount >2){
                    //双击
                    this.deleteHamburger();
                    clicked=1;
                }else{
                    clickedTime.timeA=new Date();
                };
            }
            if(!this.game.singleDrag) return;
            this.game.singleDrag = false;
            if(this.parentInit) return;
            var touches = event.getTouches();
            if(touches.length >= 2) return;
            this.parentInit = this.node.parent;
            this.node.parent = cc.find('Canvas');
            var po = cc.find('Canvas').convertToNodeSpaceAR(this.parentInit.convertToWorldSpaceAR(cc.p(this.node.x,this.node.y)));
            this.node.setPosition(po);
            this.node.zIndex=99;
        },this);
        this.node.on('touchmove',function(event){
            if(!this.parentInit) return;
            var touches = event.getTouches();
            this.node.x += touches[0].getDelta().x;
            this.node.y += touches[0].getDelta().y;
        },this);
        this.node.on('touchend',function(event){
            if(!this.parentInit) return;
            if(this.place){
                if(this.placeNode.getComponent('guest').accounts('hamburger',this.MakeContent)){
                    this.game.singleDrag = true;
                    this.deleteHamburger(true);
                    return;
                }
            }
            this.node.parent = this.parentInit;
            this.node.setPosition(0,8);
            this.parentInit = null;
            this.game.singleDrag = true;
        },this);
        this.node.on('touchcancel',function(event){
            if(!this.parentInit) return;
            if(this.place){
                if(this.placeNode.getComponent('guest').accounts('hamburger',this.MakeContent)){
                    this.game.singleDrag = true;
                    this.deleteHamburger(true);
                    return;
                }
            }
            this.node.parent = this.parentInit;
            this.node.setPosition(0,8);
            this.parentInit = null;
            this.game.singleDrag = true;
        },this)
    },
    //用来拖拽至顾客的检测
    onCollisionEnter: function (other, self) {
        console.log('enter',other.node.group)
        if(other.node.group == 'person'){
            this.place = true;
            this.placeNode = other.node;
        }
    },
    onCollisionExit: function (other, self) {
        console.log('exit',other.node.group)
        if(other.node.group == 'person'){
            this.place = false;
        }
    },
    placeVegetable(type){
        this.MakeContent += type;
        console.log(type);
        var veg = cc.instantiate(this.hamburgerChild[type]);
        this.node.addChild(veg);
        this.node.sortAllChildren();
    },
    placeMeat(){
        var meat = cc.instantiate(this.hamburgerChild[0]);
        this.MakeContent += 0;
        this.node.addChild(meat);
        this.node.sortAllChildren();
    },
    deleteHamburger(bol){
        var price = 0;
        for(var i = 1; i< this.node.childrenCount -1;i++){
            price += cc.configData.material[this.node.children[i].name].price[cc.configData.material[this.node.children[i].name].level];
        }
        price +=cc.configData.material.hamburger.price[cc.configData.material.hamburger.level]; //减去汉堡本身的价值；
        console.log('hamburger',price);
        if(bol){
            cc.coinWin += price;
            this.parentInit.place = true;
        }else{
            cc.coinWin -= price;
            this.node.parent.place = true;
        }
        this.node.destroy();
    },
    // update (dt) {},
});
